<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
session_start();

require_once 'includes/connect.php';
require_once 'includes/function.php';

$db = new myDBS();

$task = $_REQUEST["task"];

$id = $_REQUEST["id"];
$budget_year = $_REQUEST["budget_year"];
$panngan2_id = $_REQUEST["panngan2_id"];
$panngan3_id = $_REQUEST["panngan3_id"];
$ex1_id = $_REQUEST["ex1_id"];
$ex2_id = $_REQUEST["ex2_id"];
$ex3_id = $_REQUEST["ex3_id"];
$month_year = $_REQUEST["month_year"];
$amount = $_REQUEST["amount"];
$amount1 = $_REQUEST["amount1"];
$tax = $_REQUEST["tax"];
$tax1 = $_REQUEST["tax1"];
$tt = $_REQUEST["tt"];
$tt1 = $_REQUEST["tt1"];
$penalty = $_REQUEST["penalty"];
$net = $_REQUEST["net"];
$receiver = $_REQUEST["receiver"];
$comment = $_REQUEST["comment"];
$date_create = $_REQUEST["date_create"];
$proj_name = $_REQUEST["proj_name"];

$cancel_date = $_REQUEST["cancel_date"];
$cancel_reason = $_REQUEST["cancel_reason"];

//$lvl1_id = $_SESSION["lvl1_id"];

$_SESSION["lvl1_id"] = $_REQUEST["lvl1_id"];
$lvl1_id = $_REQUEST["lvl1_id"];

switch ($task) {
    case "new":
        $pet_id = $db->get_running($budget_year);

        $budget = get_budget($panngan2_id, $panngan3_id, $ex1_id, $ex2_id, "", $budget_year);

        $budget_use = get_budget_used($panngan2_id, $panngan3_id, $ex1_id, $ex2_id, $ex3_id, $budget_year);

        $budget_remain = $budget - $budget_use;
        $budget_remain_this = $budget_remain - $net;
//        echo $pet_id;
        $sql = "INSERT INTO tbl_pet (lvl1_id, panngan2_id, panngan3_id, ex1_id, ex2_id, ex3_id, month_year, budget_year, pet_id"
                . ", amount, amount1, tax, tt, tax1, tt1, penalty, net, receiver, comment, date_create "
                . ", budget_remain, budget_remain_this, proj_name  )"
                . " VALUES ('$lvl1_id', '$panngan2_id', '$panngan3_id', '$ex1_id', '$ex2_id', '$ex3_id', '$month_year', '$budget_year', '$pet_id' "
                . ", '$amount', '$amount1' , '$tax', '$tt', '$tax1', '$tt1', '$penalty', '$net', '$receiver', '$comment', '$date_create'"
                . " , '$budget_remain', '$budget_remain_this', '$proj_name')";

//        echo $sql;
        $db->runSQL($sql) or die(mysql_error());

        break;

    case "edit":
        $sql = "UPDATE tbl_pet "
                . "SET panngan2_id = '$panngan2_id' "
                . ", panngan3_id = '$panngan3_id' "
                . ", ex1_id = '$ex1_id' "
                . ", ex2_id = '$ex2_id' "
                . ", ex3_id = '$ex3_id' "
                . ", month_year = '$month_year' "
                . ", budget_year = '$budget_year' "
                . ", amount = '$amount' "
                . ", amount1 = '$amount1' "
                . ", tax = '$tax' "
                . ", tt = '$tt' "
                . ", tax1 = '$tax1' "
                . ", tt1 = '$tt1' "
                . ", penalty = '$penalty' "
                . ", net = '$net' "
                . ", receiver = '$receiver' "
                . ", comment = '$comment' "
                . ", date_create = '$date_create' "
                . ", proj_name = '$proj_name' "
                . " WHERE id = '$id' ";

        //echo $sql;
        $db->runSQL($sql) or die(mysql_error());

        break;

    case "cancel":
        $sql = "UPDATE tbl_pet "
                . " SET cancel_date = '$cancel_date', "
                . " cancel_reason = '$cancel_reason', "
                . " status = 'ยกเลิก' "
                . " WHERE id = '$id'";
        //echo $sql;

        $db->runSQL($sql) or die(mysql_error());
        break;

    default:
        break;
}
?>

